package com.example.feedback.adapter;


import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.feedback.R;
import com.example.feedback.service.entity.SuggestListBean;

import java.util.ArrayList;
import java.util.List;


/**
 * 反馈列表适配器
 */
public class SuggestListAdapter extends RecyclerView.Adapter<SuggestListAdapter.ViewHolder> {

    private List<SuggestListBean.DataDTO> data = new ArrayList<>();
    private SuggestListBean.DataDTO dataDTO;
    public SuggestListAdapter() {

    }

    public List<SuggestListBean.DataDTO> getData() {
        return data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.gt_item_suggest,viewGroup,false);
        view.setOnClickListener(v -> {
            if (itemClick != null){
                itemClick.itemClick(i);
            }
        });
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        dataDTO = data.get(i);
        viewHolder.tvTime.setText(dataDTO.getAddtime());
        String temp = "待回复";
        switch (dataDTO.getStatus()) {
            case 0:
                temp = "待回复";
                viewHolder.tvState.setTextColor(Color.parseColor("#FF584B"));
                break;
            case 1:
                temp = "已回复";
                viewHolder.tvState.setTextColor(Color.parseColor("#FF584B"));
                break;
            case 2:
                temp = "已解决";
                viewHolder.tvState.setTextColor( Color.parseColor("#7E7E7E"));
                break;

        }
        viewHolder.tvState.setText(temp);/** 状态："**/
        viewHolder.tvType.setText("意见反馈");/** 类型： **/
        viewHolder.tvTitle.setText(dataDTO.getTitle());/** 标题： **/
        if (dataDTO.getContent().length()>=10){
            viewHolder.tvContent.setText(dataDTO.getContent().substring(0, 10) + "...");/** 内容： **/
        }else {
            viewHolder.tvContent.setText(dataDTO.getContent());/** 内容： **/
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setData(List<SuggestListBean.DataDTO> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTime;
        TextView tvState;
        TextView tvType;
        TextView tvTitle;
        TextView tvContent;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvState = itemView.findViewById(R.id.tv_state);
            tvType = itemView.findViewById(R.id.tv_type);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvContent = itemView.findViewById(R.id.tv_content);
        }
    }

    private ItemClick itemClick;

    public void setItemClick(ItemClick itemClick) {
        this.itemClick = itemClick;
    }

    public interface ItemClick{
        void itemClick(int position);
    }
}
